const reqlib = require('app-root-path').require
const numberUtil = reqlib('/app/util/numberUtil')

// 计算跌的数量
function countDown (netChangeRatioList, all, has) {
  let rate = 0
  const last = netChangeRatioList[all - 1]
  if (netChangeRatioList[0] < 0 && last < 0) {
    let count = 0
    for (let i = 0; i < all; i++) {
      if (netChangeRatioList[i] < 0) {
        count++
      }
      rate += netChangeRatioList[i]
    }
    if (count >= has) {
      return {
        rate,
        flag: true
      }
    }
  }
  return {
    flag: false
  }
}
// 计算涨的数量
function countUp (netChangeRatioList, all, has) {
  let rate = 0
  const last = netChangeRatioList[all - 1]
  if (netChangeRatioList[0] > 0 && last > 0) {
    let count = 0
    for (let i = 0; i < all; i++) {
      if (netChangeRatioList[i] > 0) {
        count++
      }
      rate += netChangeRatioList[i]
    }
    if (count >= has) {
      return {
        rate,
        flag: true
      }
    }
  }
  return {
    flag: false
  }
}

function Flag (config) {
  this.key = config.key
  this.ema5 = config.ema5
  this.ema10 = config.ema10
  this.ema20 = config.ema20
  this.kdj = config.kdj
  this.kline = config.kline
  this.netChangeRatioList = config.netChangeRatioList
  this.upDown = this.getUpDownCount()
}

Flag.prototype = {
  getUpDownCount () {
    const netChangeRatioListLarge = [...this.netChangeRatioList]
    netChangeRatioListLarge.reverse()
    return {
      down1: countDown(netChangeRatioListLarge, 1, 1),
      down2: countDown(netChangeRatioListLarge, 2, 2),
      down3: countDown(netChangeRatioListLarge, 3, 3),
      down4: countDown(netChangeRatioListLarge, 4, 4),
      down5: countDown(netChangeRatioListLarge, 5, 5),
      down6: countDown(netChangeRatioListLarge, 6, 6),
      down7: countDown(netChangeRatioListLarge, 7, 7),
      down8: countDown(netChangeRatioListLarge, 8, 8),
      down9: countDown(netChangeRatioListLarge, 9, 9),
      up1: countUp(netChangeRatioListLarge, 1, 1),
      up2: countUp(netChangeRatioListLarge, 2, 2),
      up3: countUp(netChangeRatioListLarge, 3, 3),
      up4: countUp(netChangeRatioListLarge, 4, 4),
      up5: countUp(netChangeRatioListLarge, 5, 5),
      up6: countUp(netChangeRatioListLarge, 6, 6),
      up7: countUp(netChangeRatioListLarge, 7, 7),
      up8: countUp(netChangeRatioListLarge, 8, 8),
      up9: countUp(netChangeRatioListLarge, 9, 9),
      down65: countDown(netChangeRatioListLarge, 6, 5),
      down76: countDown(netChangeRatioListLarge, 7, 6),
      down87: countDown(netChangeRatioListLarge, 8, 7),
      down86: countDown(netChangeRatioListLarge, 8, 6),
      down98: countDown(netChangeRatioListLarge, 9, 8),
      down97: countDown(netChangeRatioListLarge, 9, 7)
    }
  },
  /**
   * --------------------买入信号--------------------------
   */
  // 1/3
  buyManyDownBig () {
    return (
      this.upDown.down98.flag ||
      this.upDown.down5.flag ||
      this.upDown.down97.flag ||
      this.upDown.down87.flag ||
      this.upDown.down76.flag
    )
  },
  // 可以买1/6
  buyManyDown () {
    return (
      this.upDown.down4.flag ||
      this.upDown.down86.flag ||
      this.upDown.down65.flag
    )
  },
  // 一天跌很多，第二天涨了就卖，如果跌只跌一点点，那也可以等一等
  buyOneDeepDown () {
    if (this.key === 'ETH') {
      return this.kline.netChangeRatio < -13
    }
    if (this.key === 'BNB') {
      return this.kline.netChangeRatio < -20
    }
    if (this.key === 'BTC') {
      return this.kline.netChangeRatio < -10
    }
  },
  buyOpenIsHigh1 () {
    // 买1/3
    const diff = numberUtil.countDifferenceRate(this.kline.high, this.kline.open)
    const rate = Math.abs(diff)
    return this.kline.netChangeRatio < 0 && rate < 0.1 && this.ema10 > this.ema20
  },
  buyHighLowerEma5 () {
    return this.kline.high < this.ema5 && this.ema5 < this.ema10
  },
  buyHighWave () {
    const diff = numberUtil.countDifferenceRate(this.kline.open, this.kline.low)
    return diff > 20
  },
  // 卖
  sellLowWave () {
    // ema5 < ema10 卖1/2
    // 1/6
    let flag = true
    const netChangeRatioListLarge = [...this.netChangeRatioList]
    netChangeRatioListLarge.reverse()
    netChangeRatioListLarge.forEach((v, index) => {
      const rate = Math.abs(v)
      if (index < 3) {
        if (rate > 2) {
          flag = false
        }
      }
    })
    return flag
  },
  // 完结
  sellOpenIsLow (r) {
    // 0.1, 卖1/2
    // 0.3, 卖1/4
    const diff = numberUtil.countDifferenceRate(this.kline.low, this.kline.open)
    const rate = Math.abs(diff)
    return this.kline.netChangeRatio > 0 && rate < r && this.ema5 > this.ema10
  },
  sellLowUp (rate) {
    // -6 卖出1/2
    // -4 卖出1/6
    const diff = numberUtil.countDifferenceRate(this.kline.low, this.kline.open)
    return this.kline.netChangeRatio > 0 && diff < rate
  },
  // 1/6
  sellTest2 () {
    // 卖出信号，效果好像还行，有待验证
    return this.ema5 > this.ema20 && this.ema20 > this.ema10
  },
  // 1/6
  sellTest3 () {
    // ema5 < ema10 < ema20
    return this.ema5 < this.ema10 && this.ema20 > this.ema10 && this.kline.close > this.ema10 && this.kline.open < this.ema20 && this.kline.netChangeRatio < 0
  },
  // 1/6
  sellTest4 () {
    // ema5 < ema10 < ema20
    return this.ema5 < this.ema10 && this.ema20 > this.ema10 && this.kline.close > this.ema10 && this.kline.open < this.ema5 && this.kline.close < this.ema20
  },
  // 1/6
  sellTest5 () {
    // ema5 < ema10 < ema20
    return this.ema5 < this.ema10 && this.ema20 > this.ema10 && this.kline.close > this.ema10 && this.kline.open > this.ema10 && this.kline.close < this.ema20 && this.kline.open < this.ema20
  },
  // 1/6
  sellTest6 () {
    // ema10 < ema20 < ema5
    return this.ema5 > this.ema20 && this.ema20 > this.ema10 && this.kline.low > this.ema5
  }
}

module.exports = Flag
